{{extend "tasks_layout.html"}}

{{block content_tool}}
<a class="btn btn-green" href="/tasks/develop/{{=task_id}}#plan">返回任务</a>
{{end}}

{{block content_main}}
{{use "jquery", ui=True}}
{{use "facebox"}}
{{use "jqutils", ajaxForm=True}}
{{use "functional"}}
{{use "jqmultiselect"}}
<style>
#plan .disabled {
    opacity:0.4;
    color: #000 !important;
    color: #ccc; 
}
</style>
<div class="box box-panel">
    <h2>计划修改</h2>
    <div class="box-body">
        <div id="plan">
            <table class="table fix-header" id="plan_table">
                <thead>
                <tr>
                <th></th>
                <th>类型</th>
                <th>启用</th>
                <th>名称</th>
                <th>阶段分类</th>
                <th>公共</th>
                <th>计划开始时间</th>
                <th>计划结束时间</th>
                <th>计划系统</th>
                <th>执行系统</th>
                <th>责任人</th>
                <th>说明</th>
                <th>操作</th>
                </tr>
                </thead>
                {{<< plan_tbody}}
            </table>
            <div class="box-bottom">
                <a id="plan_add" href="javascript:void(0)" class="btn btn-blue">添加里程碑/阶段</a>
                <a id="pre_submit" href="javascript:void(0)" class="btn btn-green" title="计划只有提交后才会真正生效">计划提交</a>
                <a href="/tasks/plan/reset/{{=task_id}}" class="btn btn-green" title="将计划恢复成当前正在执行计划" onclick="return confirm('确定要重置当前计划吗？');">计划重置</a>
            </div>
            
            <script>
                var post_planedit = function(r, prefix, task_id){
                    prefix = prefix || '#plan_tr_';
                    if (r.success){
                        show_message(r.message);
                        jQuery(document).trigger('close.facebox');
                        var tr_id = prefix+r.data.id;
                        $(tr_id).replaceWith(r.data.data);
                        $(tr_id).effect('highlight', 5000);
                        $(tr_id+' a.plan_edit').click(process_plan_click);
                        $(tr_id+' a.plan_delete').click(process_plan_delete);
                    } else if (!r.success){
                        $('div.error-div').remove();
                        show_message(r.message, 'error');
                        $.each(r.data, function(key, value){
                            var f, t, e, d;
                            f = 'field_' + key;
                            t = $('label[for='+f+']');
                            e = $('<div class="ui-state-error error-div" style="border:0;"><span class="ui-icon ui-icon-info" style="float:left;"></span><span class="ui-state-error-text">' + value + '</span></div>');
                            d = t.position();
                            t.after(e);
                        });
                    }
                }

                var post_planadd = function(r, prefix, task_id){
                    prefix = prefix || '#plan_tr_';
                    if (r.success){
                        show_message(r.message);
                        jQuery(document).trigger('close.facebox');
                        var tr_id = prefix+r.data.id;
                        $('#plan_table').find('tbody').append(r.data.data);
                        $(tr_id).effect('highlight', 5000);
                        $(tr_id+' a.plan_edit').click(process_plan_click);
                        $(tr_id+' a.plan_delete').click(process_plan_delete);
                        
                    } else if (!r.success){
                        $('div.error-div').remove();
                        show_message(r.message, 'error');
                        $.each(r.data, function(key, value){
                            var f, t, e, d;
                            f = 'field_' + key;
                            t = $('label[for='+f+']');
                            e = $('<div class="ui-state-error error-div" style="border:0;"><span class="ui-icon ui-icon-info" style="float:left;"></span><span class="ui-state-error-text">' + value + '</span></div>');
                            d = t.position();
                            t.after(e);
                        });
                    }
                }
                
                var process_plan_click = function(){
                    var a = $(this);
                    var url = '/tasks/plan/change/'+a.attr('rel');
                    $.facebox(function() {
                        $.get(url, function(data) {
                            $.facebox(data);
                            $('#planedit_form').ajaxForm(post_planedit.partial(_, '#plan_tr_', a.attr('rel')));
                        });
                    });
                }
                
                var process_plan_add = function(){
                    var a = $(this);
                    var url = "/tasks/plan/add/{{=task_id}}"
                    $.facebox(function() {
                        $.get(url, function(data) {
                            $.facebox(data);
                            $('#planedit_form').ajaxForm(post_planadd.partial(_, '#plan_tr_', a.attr('rel')));
                        });
                    });
                }
                
                var process_plan_delete = function(){
                    var a = $(this);
                    var url = "/tasks/plan/delete/" + a.attr('rel');
                    var tr = '#plan_tr_' + a.attr('rel');
                    $.ajax({
                        type: 'POST',
                        url: url,
                        dataType: 'json',
                        success: function(r){
                            if(r.success){
                                show_message(r.message);
                                $(tr).remove();
                            }else{
                                show_message(r.message, 'error');
                            }
                        }
                    });
                }
                
                $(function(){
                    $.facebox.settings.loadingImage = '{{=url_for_static('facebox/loading.gif')}}'
                    $.facebox.settings.closeImage = '{{=url_for_static('facebox/closelabel.png')}}'
                    $('#plan_table a.plan_edit').click(process_plan_click);
                    $('#plan_add').click(process_plan_add);
                    $('#plan_table a.plan_delete').click(process_plan_delete);
                });
            </script>
        </div>
    </div>
    <script>
        $(function(){
            $('#pre_submit').click(function(){
                $.facebox(function() {
                    var url = "/tasks/plan/submit/{{=task_id}}";
                    $.ajax({
                        type: 'POST',
                        dataType: 'html',
                        url: url,
                        success: function(data) {
                            $.facebox(data);
                        }
                    });
                });
            });
        });
    </script>
</div>
{{end}}